<?php
/*
Page:           db.php
Created:        Aug 2006
Last Mod:       Mar 18 2007
This page handles the database update if the user
does NOT have Javascript enabled.	
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
Licensed under a Creative Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/
See credits.txt for full credit details.
--------------------------------------------------------- */

header("Cache-Control: no-cache");
header("Pragma: nocache");

require(  dirname( dirname( dirname( dirname(__FILE__ )))).'/config/config.inc.php' );
require(  dirname( dirname(__FILE__ )).'/productratingtime.php' );
require('_config-rating.php');

$productrating_time = new productratingtime();

// getting the values
$vote_sent_time = preg_replace('/[^0-9]/', '', $_REQUEST['j_time']);
$vote_sent_time = (int)$vote_sent_time;
$id_sent_time = preg_replace('/[^0-9a-zA-Z]/', '', $_REQUEST['q_time']);
$id_sent_time = (int)$id_sent_time;
$ip_num_time = preg_replace('/[^0-9\.]/', '', $_REQUEST['t_time']);
$units_time = preg_replace('/[^0-9]/', '', $_REQUEST['c_time']);
$units_time = (int)$units_time;
$ip_time = $_SERVER['REMOTE_ADDR'];
$referer_time = $_SERVER['HTTP_REFERER'];

// kill the script because normal users will never see this.
if ($vote_sent_time > $units_time) 
	die('Sorry, vote appears to be invalid.');

// connecting to the database to get some information
$query_time = mysql_query("SELECT `total_votes`, `total_value`, `used_ips` FROM `$rating_dbname_time`.`$rating_tableName_time` WHERE `id` = '$id_sent_time'") or die("Error: ".mysql_error());
$numbers_time = mysql_fetch_assoc($query_time);
$checkIP_time = unserialize($numbers_time['used_ips']);
$count_time = $numbers_time['total_votes']; // how many votes total
$current_rating_time = $numbers_time['total_value']; // total number of rating added together and stored
$sum_time = $vote_sent_time + $current_rating_time; // add together the current vote value and the total vote value
$tense_time = ($count_time == 1) ? $productrating_time->traduz('vote') : $productrating_time->traduz('votes'); // plural form votes/vote

// checking to see if the first vote has been tallied
// or increment the current number of votes
($sum_time == 0 ? $added_time = 0 : $added_time = $count_time + 1);

// if it is an array i.e. already has entries the push in another value
((is_array($checkIP_time)) ? array_push($checkIP_time, $ip_num_time) : $checkIP_time = array($ip_num_time));
$insertip_time = serialize($checkIP_time);

// IP check when voting
$voted_time = mysql_num_rows(mysql_query("SELECT `used_ips` FROM `$rating_dbname_time`.`$rating_tableName_time` WHERE `used_ips` LIKE '%".$ip_time."%' AND `id` = '".$id_sent_time."'"));
echo  "hung : $query_time";
if (!$voted_time) 
{     
	// if the user hasn't yet voted, then vote normally...
	if (($vote_sent_time >= 1 && $vote_sent_time <= $units_time) /*&& ($ip_time == $ip_num_time)*/) 
	{ 
		// keep votes within range
		$update_time = "UPDATE `$rating_dbname_time`.`$rating_tableName_time` SET `total_votes` = '".$added_time."', `total_value` = '".$sum_time."', `used_ips` = '".$insertip_time."' WHERE `id` = '$id_sent_time'";
		$result_time = mysql_query($update_time);		
                echo "hung1: $update_time";
    } 
    
    header("Location: $referer_time"); 
    exit;
}